package ExpressFive;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class InterfaceFive extends JFrame implements ActionListener {
    MyQueue<JButton> btn1_queue, btn2_queue, btn3_queue, btn4_queue;
    MultiRespondQueue multiRespondQueue;
    JPanel panel1,panel2,panel3,panel4;
    JTextArea textArea;
    int processCount=0;
    InterfaceFive(){

        multiRespondQueue=new MultiRespondQueue();
        setLayout(new GridLayout(1,2));
        init();
        init_menu();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBounds(100,100,600,300);
        setVisible(true);
    }
    void init(){
        panel1=new JPanel(new GridLayout(1,1));
        panel2=new JPanel(new GridLayout(1,1));
        panel3=new JPanel(new GridLayout(1,1));
        panel4=new JPanel(new GridLayout(1,1));

        panel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),"一级队列，时间片t=1"));
        panel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),"二级队列，时间片t=2"));
        panel3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),"三级队列，时间片t=4"));
        panel4.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black),"四级队列，时间片t=8"));

        btn1_queue=new MyQueue<>();
        btn2_queue =new MyQueue<>();
        btn3_queue =new MyQueue<>();
        btn4_queue =new MyQueue<>();
        JPanel panel=new JPanel(new GridLayout(4,1));
        panel.add(panel1);
        panel.add(panel2);
        panel.add(panel3);
        panel.add(panel4);
        textArea=new JTextArea();
        textArea.setBorder(BorderFactory.createTitledBorder("进程提示"));
        textArea.setDisabledTextColor(Color.black);
        textArea.setEnabled(false);
        add(panel);
        add(new JScrollPane(textArea));

        Thread thread=new Thread(()->{
            while(true) {
                flash_show();
            }
        });
        thread.start();
    }
    void init_menu(){
        JMenuBar jMenuBar=new JMenuBar();
        JMenu jMenu=new JMenu("菜单");
        jMenuBar.add(jMenu);
        JMenuItem item1=new JMenuItem("run");
        JMenuItem item2=new JMenuItem("add process");
        JMenuItem item3=new JMenuItem("interrupt");
        item1.addActionListener(this);
        item2.addActionListener(this);
        item3.addActionListener(this);
        jMenu.add(item1);
        jMenu.add(item2);
        jMenu.add(item3);
        jMenu.addActionListener(this);
        setJMenuBar(jMenuBar);

    }
    void add_btn(MyProcess p){
        multiRespondQueue.add(p);
        btn1_queue.add(p.getBtn());
        Thread thread=new Thread(()->{
            textArea.setText(textArea.getText()+"\n"+p);
        });
        thread.start();
    }
    void flash_show(){

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        panel1.removeAll();
        panel2.removeAll();
        panel3.removeAll();
        panel4.removeAll();

        for(int i=0;i<multiRespondQueue.getQ1().size();i++){
            panel1.add(multiRespondQueue.getQ1().get(i).getBtn());
        }
        for(int i=0;i<multiRespondQueue.getQ2().size();i++){
            panel2.add(multiRespondQueue.getQ2().get(i).getBtn());
        }
        for(int i=0;i<multiRespondQueue.getQ3().size();i++){
            panel3.add(multiRespondQueue.getQ3().get(i).getBtn());
        }
        for(int i=0;i<multiRespondQueue.getQ4().size();i++){
            panel4.add(multiRespondQueue.getQ4().get(i).getBtn());
        }
        panel1.updateUI();
        panel2.updateUI();
        panel3.updateUI();
        panel4.updateUI();
        /*panel1.repaint();
        panel2.repaint();
        panel3.repaint();
        panel4.repaint();*/
    }

    public static void main(String[] args) {
        new InterfaceFive();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()) {
            case "run":
                multiRespondQueue.setFlag(true);
                Thread thread=new Thread(()->{
                    multiRespondQueue.run();
                });
                thread.start();
                System.out.println("run");
                break;
            case "add process":
                /*String str1=JOptionPane.showInputDialog(this,"请输入进程id","输入id",
                        JOptionPane.INFORMATION_MESSAGE);*/
                String str2=JOptionPane.showInputDialog(this,"请输入进程需要时间","输入时间",
                        JOptionPane.INFORMATION_MESSAGE);
                String str3=JOptionPane.showInputDialog(this,"请输入进程优先级","输入优先级",
                        JOptionPane.INFORMATION_MESSAGE);
                if(str2!=null&&!str2.equals("")&&str3!=null&&!str3.equals("")) {
                    int priority = 0;
                    boolean flag=false;
                    try {
                        priority=Integer.parseInt(str3);
                    }catch (Exception exception){
                        flag=true;
                        exception.printStackTrace();
                    }
                    if(!flag) {
                        Color[] colors = {Color.black, Color.DARK_GRAY, Color.GREEN, Color.PINK, Color.RED, Color.YELLOW, Color.BLUE};
                        Random random = new Random();

                        add_btn(new MyProcess(String.valueOf(processCount), Integer.parseInt(str2),priority, colors[Math.abs(random.nextInt() % colors.length)]));
                        processCount++;
                        multiRespondQueue.setFlag(true);
                        System.out.println("process add");
                    }
                }
                break;
            case "interrupt":
                multiRespondQueue.setFlag(false);
                System.out.println("interrupt process run");
                break;
        }
    }
}
